
#include <stddef.h>
#include <string.h>
#include "MessageDefine.h"
#include "BlockPool.h"
#include "mbed_critical.h"

#define MsgPool_EnterCriticel()		core_util_critical_section_enter()
#define MsgPool_ExitCriticel()		core_util_critical_section_exit()

#define MsgPool_SIZE		32

static Message_T msgPoolStore[MsgPool_SIZE];
static BPool msgPool;

void MsgPool_Init(void){
	BPool_Init(&msgPool, msgPoolStore, sizeof(msgPoolStore), sizeof(Message_T));
}

Message_T* Message_Clone(Message_T* msg){
	Message_T* m = (Message_T*)BPool_Get(&msgPool);
	if(!m){
		return m;
	}
	*m = *msg;
	return m;
}

Message_T* Message_New(MsgBusAddr_T dest, MsgBusAddr_T src, MsgType_T type, MsgAttr_T attr){
	return Message_Clone(&(Message_T){dest, src, type, attr,});
}

void Message_Del(Message_T* msg){
	if(&msgPoolStore[0] <= msg && msg < &msgPoolStore[MsgPool_SIZE]){
		BPool_Put(&msgPool, msg);
	}
}


